﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Reflection;
using YXQ.Common.Web;
using Microsoft.Practices.Unity;
using YXQ.Common.Helper;
using System.Web;
using System.Configuration;

namespace YXQ.Service
{
    public class ManagerWebInit
    {
        protected static BindingFlags BINDING_FLAGS_SET
                 = BindingFlags.Public
                 | BindingFlags.SetProperty
                 | BindingFlags.Instance
                 | BindingFlags.SetField
                 | BindingFlags.Static
                 ;
        public static IUnityContainer ApplicationContainer
        {
            get
            {
                return WebSingleton.ApplicationContainer;
            }
        }
        public static void InitUnity(IUnityContainer container)
        {

            if (WebSingleton.ApplicationContainer == null)
            {

                //IOC容器注册
                try
                {
                    object obj;
                    Type type = Type.GetType("YXQ.Service.CenterService,YXQ.Service");
                    Type formType;
                    Type toType;

                    PropertyInfo[] properties = type.GetProperties(BINDING_FLAGS_SET);
                   
                    WebSingleton.ApplicationContainer = container;

                    ApplicationContainer.BuildUp(typeof(ManagerWebInit));


                    foreach (PropertyInfo propertie in properties)
                    {
                        string pname = propertie.Name;
                        string IServiceName = pname;
                        XElement ioc = GetServiceName(IServiceName);
                        if (ioc != null)
                        {
                            formType = Type.GetType(ioc.Attribute("IService").Value);
                            toType = Type.GetType(ioc.Attribute("Service").Value);
                            container.RegisterType(formType, toType);
                            obj = WebSingleton.ApplicationContainer.Resolve(formType);

                            propertie.SetValue(typeof(ManagerWebInit), obj, null);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error("错误信息：" + ex.Message);
                    throw (ex);
                }



            }
            

        }
        /// <summary>
        /// 根据XML获取IOC配置
        /// </summary>
        /// <param name="IServiceName"></param>
        /// <returns></returns>
        private static XElement GetServiceName(string IServiceName)
        {
            XElement config = XElement.Load(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["IOCConfig"]));
            var tables = from ele in config.Elements("IOCRegister")
                         where ele.Attribute("id").Value == IServiceName
                         select ele;

            return tables.FirstOrDefault();
        }
    }
}
